import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:shenlingwuliu_flutter/utils/tokenManager.dart';

class ProfilePage extends StatefulWidget {
  const ProfilePage({super.key});

  @override
  State<ProfilePage> createState() => _ProfilePageState();
}

class _ProfilePageState extends State<ProfilePage> {
  List profileList = [
    {
      "title": "换绑手机",
    },
    {
      "title": "修改密码",
    },
    {
      "title": "消息通知设置",
    },
    {
      "title": "清理缓存",
    },
  ];

  void _quit() {
    TokenManager().removeToken();
    Navigator.of(context)
        .pushNamedAndRemoveUntil('/login', ModalRoute.withName('/login'));
  }

  void quitToast() {}
  AlertDialog _buildDialog() {
    return AlertDialog(
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
        backgroundColor: Colors.white,
        content: const Text(
          '确定要退出登录吗？',
          textAlign: TextAlign.center,
        ),
        actions: <Widget>[
          Row(
            children: [
              Expanded(
                  child: TextButton(
                onPressed: () {
                  Navigator.of(context).pop();
                },
                child: const Text(
                  '取消',
                  style: TextStyle(color: Colors.grey),
                ),
              )),
              Expanded(
                  child: TextButton(
                      child:
                          const Text('退出', style: TextStyle(color: Colors.red)),
                      onPressed: () async {
                        _quit();
                      }))
            ],
          )
        ]);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
          title: const Text('系统设置'),
          backgroundColor: Colors.white,
          centerTitle: true,
          leading: IconButton(
              onPressed: () {
                Navigator.pop(context);
              },
              icon: const Icon(Icons.arrow_back_ios))),
      body: Column(
        children: profileList.asMap().entries.map((entry) {
          final index = entry.key;
          final item = entry.value;
          return Container(
              padding: const EdgeInsets.all(16),
              child: Column(
                children: [
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Text(item['title'],
                          style: const TextStyle(
                              fontSize: 16, fontWeight: FontWeight.w500)),
                      SvgPicture.asset(
                        'assets/images/ic_btn_more.svg',
                        width: 22,
                      )
                    ],
                  ),
                  if (index == profileList.length - 1)
                    Container(
                      margin: const EdgeInsets.only(top: 20),
                      child: ElevatedButton(
                          onPressed: () {
                            showDialog(
                                context: context,
                                builder: (BuildContext context) {
                                  return _buildDialog();
                                });
                          },
                          style: const ButtonStyle(
                              backgroundColor:
                                  WidgetStatePropertyAll<Color>(Colors.white)),
                          child: Container(
                            padding: const EdgeInsets.only(right: 10, left: 10),
                            width: double.infinity,
                            height: 40,
                            child: const Center(
                              child: Text(
                                '退出',
                                style: TextStyle(
                                    color: Colors.black, fontSize: 16),
                              ),
                            ),
                          )),
                    )
                ],
              ));
        }).toList(),
      ),
    );
  }
}
